package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.QuotationProductDao;
import com.xbongbong.saas.domain.entity.QuotationProductEntity;
import com.xbongbong.saas.model.QuotationProductModel;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/8/16 10:20
 * @since v1.0
 */
@Service("quotationProductModel")
public class QuotationProductModelImpl implements QuotationProductModel {
    @Resource
    private QuotationProductDao quotationProductDao;

    @Override
    public Integer insert(QuotationProductEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        return quotationProductDao.insert(entity);
    }

    @Override
    public Integer update(QuotationProductEntity entity) throws XbbException {
        long now = DateTimeUtil.getInt();
        entity.setUpdateTime(now);
        StandardizeDataUtil.standardizeNotString(entity);
        return quotationProductDao.update(entity);
    }

    @Override
    public List<QuotationProductEntity> findEntitys(Map<String, Object> param) {
        return quotationProductDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return quotationProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public QuotationProductEntity getByKey(Long key, String corpid) {
        return quotationProductDao.getByKey(key, corpid);
    }

    @Override
    public Integer save(QuotationProductEntity entity) throws XbbException {
        Long id = entity.getId();
        if (id == null || Objects.equals(id, 0L)) {
            return insert(entity);
        } else {
            return update(entity);
        }
    }

    @Override
    public List<QuotationProductEntity> getProductsByQuoId(Long quoId, String corpid, List<Integer> delIn) {
        // 获取关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("delIn", delIn);
        params.put("quotationId", quoId);
        params.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(params);
    }

    @Override
    public List<QuotationProductEntity> getProductsByQuoId(Long quoId, String corpid) {
        // 获取合同关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("del", 0);
        params.put("quotationId", quoId);
        params.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        return findEntitys(params);
    }

    @Override
    public Integer deleteQuotationAllProduct(List<Long> quotationIdIn, String corpid) {
        return quotationProductDao.deleteQuotationAllProduct(quotationIdIn, corpid);
    }

    @Override
    public List<QuotationProductEntity> getQuotationProductByQuotationIdIn(String corpid, List<Long> quotationIdIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("quotationIdIn", quotationIdIn);
        param.put("del", 0);
        param.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(param);
    }

    @Override
    public Integer dataConsistencyUpdate(List<QuotationProductEntity> list, String corpid) {
        return quotationProductDao.dataConsistencyUpdate(list, corpid);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Integer isArray) {
        return quotationProductDao.isProductSubAttrExist(attr, corpid, isArray);
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn){
        return quotationProductDao.summaryByAttr(field, corpid, refId, productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Integer isArray) {
        return quotationProductDao.countProductSub(attr, corpid, isArray);
    }

    @Override
    public List<QuotationProductEntity> getProductWithSubData(String attr, String corpid, Integer isArray, Long idGte, Integer limit) {
        return quotationProductDao.getProductWithSubData(attr, corpid, isArray, idGte, limit);
    }

    @Override
    public Integer deleteBatch(List<Long> deleteList, String corpid, Integer del) {
        if (CollectionUtils.isEmpty(deleteList)) {
            return 0;
        }
        return quotationProductDao.deleteBatch(deleteList, corpid, del);
    }

    @Override
    public Integer updateBatch(List<QuotationProductEntity> list, String corpid) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        StandardizeDataUtil.standardize4QuotationProductEntityList(list);
        return quotationProductDao.updateBatch(list, corpid);
    }

    @Override
    public Integer insertBatch(List<QuotationProductEntity> insertList) throws XbbException {
        if (CollectionUtils.isEmpty(insertList)) {
            return 0;
        }
        return quotationProductDao.insertBatch(insertList);
    }
}
